<?php
require_once("model/Product.php");
require_once("db/AbstractDb.php");

class CartDAO extends AbstractDb {
	
	private function createProductInternal($pole) {

		$result = new Product();

		$result->setProductId($pole["idp"]);
		$result->setProductName($pole["jmeno"]);
		$result->setProductShortDesc($pole["kpopisek"]);
		$result->setProductLongDesc($pole["dpopisek"]);
		$result->setProductPrice($pole["cena"]);
		$result->setProductCategory($pole["cat_id"]);
		$result->setProductCount($pole["pocet"]); //added

		return $result;
	}
	
	
	public function insertItem($cartId, Product $item){
		$queryString = "INSERT INTO cart_content (idZ, pocet,idCart) VALUES ($item->getProductId(),1,$cartId)";
		$this->query($queryString);
	}
	
	public function insertItemById($cartId, $itemId){
		$queryString = "INSERT INTO cart_content (idZ, pocet,idCart) VALUES ($itemId,1,$cartId)";
		$this->query($queryString);
	}
	
	public function loadItem($cartId, $itemId){
		$queryString = "SELECT * FROM cart_content WHERE idCart=$cartId AND idZ=$itemId";
		$result = $this->query($queryString);
		
		if($this->getAffectedRows() > 0){
			
			$row = mysql_fetch_array($result);
			
			return $this->createProductInternal($row);
			
		} else {
			return null;
		}
		
	}
	
	public function removeItem($cartId, Product $item){
		$queryString = "DELETE FROM cart_content WHERE idCart=$cartId AND idZ=$item->getProductId()";
		$this->query($queryString);
	}
	
	public function removeItemById($cartId, $idItem){
		$queryString = "DELETE FROM cart_content WHERE idCart=$cartId AND idZ=$idItem";
		$this->query($queryString);
	}

	public function increaseCountOfItem($cartId, $itemId){
		$queryString = "UPDATE cart_content SET pocet=pocet+1 WHERE idCart=$cartId AND idZ=$itemId";
		$this->query($queryString);
	}
	
	public function decreaseCountOfItem($cartId, $itemId){
		$queryString = "UPDATE cart_content SET pocet=pocet+1 WHERE idCart=$cartId AND idZ=$itemId";
		$this->query($queryString);
	}
	
	public function loadAllItems($cartId){
		$queryString = "SELECT cart_content.idCart, cart_content.pocet,products.*
						FROM cart_content,products
						WHERE idCart=$cartId AND cart_content.idZ=products.idp";
		
		$result = $this->query($queryString);
		
		if($this->getAffectedRows() > 0){
		
		$array = array();
		
			while($row = mysql_fetch_array($result)) {
       			$array[] = $this->createProductInternal($row);
   			}
		} else {
			$array = null;
		}
		
		return $array;
	}
	
	public function createCart($idu, $cartId){
		$queryString = "INSERT INTO cart (id,idu) VALUES ($cartId,$idu)";
		$this->query($queryString);
	}
	
	public function removeAllItems($cartId){
		$queryString = "DELETE FROM cart_content WHERE idCart=$cartId";
		$this->query($queryString);
	}
	
	public function getCountOfItemsInCart($cartId){
		$queryString = "SELECT sum(pocet) as poc FROM cart_content WHERE idCart=$cartId";
		$result = $this->query($queryString);
		
		$row = mysql_fetch_array($result);
		
		$count = $row["poc"];
		
		if(is_null($count)){
			$count = 0;
		}
		
		return $count;
	}
	
}

?>
